package com.aubrey.threadDemo.lock;

public class MyReetrantLock {

	private boolean isLocked = false;
	
	private Thread lockBy = null;
	
	private int lockCount = 0;
	
	public synchronized void lock() {
		Thread currentThread = Thread.currentThread();
		while(isLocked && lockBy != currentThread) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		isLocked = true;
		lockCount++;
		lockBy = currentThread;
	}
	
	public synchronized void unLock() {
		if(Thread.currentThread() == lockBy) {
			lockCount--;
			if(lockCount == 0) {
				isLocked = false;
				this.notifyAll();
			}
			
		}
		
	}

}
